<template>
    <el-dialog
        :title="title"
        :close-on-click-modal="false"
        :visible.sync="visible"
        class="SHZY-dialog SHZY-dialog_center"
        lock-scroll
        width="600px"
    >
        <el-row :gutter="15">
            <el-form
                ref="elForm"
                :model="dataForm"
                size="small"
                label-width="110px"
                label-position="right"
                :rules="rules"
            >
                <el-col :span="24">
                    <el-form-item label="打印配置名称" prop="PrintConfigId">
                        <SHZY-select
                            v-model="dataForm.PrintConfigId"
                            :list="printOptions"
                            label="name"
                            value="id"
                            placeholder="打印配置名称"
                        ></SHZY-select>
                    </el-form-item>
                </el-col>
            </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                type="primary"
                @click="dataFormSubmit()"
                :loading="btnLoading"
                >确 定</el-button
            >
        </span>
    </el-dialog>
</template>
<script>
import { SetDefaultPrintConfig } from "@/api/common/printExecute";

import { getPrintConfigList } from "@/api/common/printConfig";
export default {
    components: {},
    props: {
        title: {
            type: String,
            default: "打印成品条码"
        }
    },
    data() {
        return {
            btnLoading: false,
            visible: false,
            dataForm: {
                PrintConfigId: undefined,
                EventCode: undefined
            },
            rules: {
                PrintConfigId: [
                    {
                        required: true,
                        message: "请选择模板类型",
                        trigger: '"change"'
                    }
                ]
            },
            printOptions: []
        };
    },
    watch: {
        visible(val) {
            if (!val) this.$emit("refresh");
        }
    },
    created() {
        this.getPrintList();
    },
    mounted() {},
    methods: {
        getPrintList() {
            getPrintConfigList().then(res => {
                this.printOptions = res.data.list;
            });
        },
        init(code) {
            this.dataForm.EventCode = code;
            this.visible = true;
            this.$nextTick(() => {
                this.$refs["elForm"].resetFields();
            });
        },
        dataFormSubmit() {
            this.$refs["elForm"].validate(valid => {
                if (!valid) return;
                this.save();
            });
        },
        save() {
            this.$confirm("更改默认打印配置名称, 是否继续?", "提示", {
                type: "warning"
            })
                .then(() => {
                    this.btnLoading = true;
                    SetDefaultPrintConfig(this.dataForm)
                        .then(res => {
                            this.btnLoading = false;
                            this.$emit("refresh", this.dataForm.PrintConfigId);
                        })
                        .catch(res => {
                            this.btnLoading = false;
                        });
                })
                .catch(() => {});
        }
    }
};
</script>
